<template>
  <div>
    <div style="margin-bottom: 15px;">
      <span>门诊处方</span>
      <el-button>提交</el-button>
      <el-button @click="prescriptionAddHandle">增方</el-button>
      <el-button @click="prescriptionDelHandle">删方</el-button>
      <el-button @click="drugAddHandle">增药</el-button>
      <el-button @click="drugDelHandle">删药</el-button>
    </div>
    <el-row :gutter="10">
      <el-col :span="4">
        <div v-for="(item,index) in prescriptionList" :key="'prescription' + index" class="recipel-card" :class="{active: currentIndex === index }"
          style="" @click="prescriptionClickHandle(item,index)">
          <el-row v-if="item.id">
            <el-col :span="12">{{item.prescriptionType}}</el-col>
            <el-col :span="12" style="text-align: right;">{{item.prescriptionOrderNumber}}</el-col>
            <el-col :span="12">收费</el-col>
            <el-col :span="12" style="text-align: right;">{{item.prescriptionCost}}</el-col>
          </el-row>
          <el-row v-else>
            <el-col :span="24">普通</el-col>
            <el-col :span="24">新增的处方</el-col>
          </el-row>
        </div>
      </el-col>
      <el-col :span="20">
        <div v-if="drugListVisible">
          <el-form>
            <el-row>
              <el-col :span="6">
                <el-form-item label="处方类型">
                  <el-select v-model="recipelType" clearable>
                    <el-option v-for="item in type" :value="item.value" :label="item.label" :key="item.value"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="处方金额">
                  <span>0.00</span>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="处方诊断">
                  <el-select v-model="recipelName" clearable>
                    <el-option v-for="item in type" :value="item.value" :label="item.label" :key="item.value"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-button>处方审核</el-button>
              </el-col>
            </el-row>
          </el-form>
          <div class="drugs-alert" v-if="visible">
            <!-- 药品列表操作行 -->
            <div>
              【商品列表】【第 1 页】
              <div style="float: right;">
                <el-button>上一页</el-button>
                <el-button>下一页</el-button>
                <el-button type="success" icon="el-icon-close" circle @click="drugAlertCloseHandle"></el-button>
              </div>
            </div>
            <!-- 药品列表 -->
            <div>
              <el-table :data="drugBox.list" ref="singleTable" border>
                <el-table-column type="index" width="50">
                  <template slot-scope="scope">
                    <el-radio v-model="drugBox.id" :label="scope.row.id" style="position: relative;left: 6px;top:-2px;"
                      @change="checkitem(scope.row)">&nbsp;</el-radio>
                  </template>
                </el-table-column>
                <el-table-column width="150" property="drugsName" label="通用名"></el-table-column>
                <el-table-column width="100" property="name" label="商品名"></el-table-column>
                <el-table-column property="address" label="单位"></el-table-column>
                <el-table-column property="address" label="价格"></el-table-column>
                <el-table-column property="address" label="剩余库存"></el-table-column>
              </el-table>
            </div>
            <!-- 药品说明 -->
            <div>

            </div>
          </div>
          <el-table ref="multipleTable" border :data="drugList" tooltip-effect="dark" style="width: 100%"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="35"></el-table-column>
            <el-table-column label="药品名称" width="120">
              <template slot-scope="scope">
                <el-input slot="reference" v-model="scope.row.drugsName" @focus="inputFocusHandle(scope.row)" @input="inputChange(scope.row)"></el-input>
              </template>
            </el-table-column>
            <!-- <el-table-column prop="name" label="规格" width="120"></el-table-column> -->
            <el-table-column prop="dose" label="单次量"></el-table-column>
            <el-table-column prop="portion" label="份/数"></el-table-column>
            <el-table-column prop="dosage" label="发药量"></el-table-column>
            <el-table-column prop="usage" label="用法"></el-table-column>
            <el-table-column prop="frequency" label="频次"></el-table-column>
            <!-- <el-table-column prop="address" label="天数"></el-table-column> -->
            <el-table-column prop="entrust" label="用药嘱托" show-overflow-tooltip></el-table-column>
          </el-table>

          <div>

          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        drugListVisible: false,
        recipelType: '',
        recipelName: '',
        value: '',
        type: [{
            value: '1',
            label: '普方'
          },
          {
            value: '2',
            label: '专家'
          }
        ],
        currentIndex: '',
        prescriptionList: [], // 处方列表
        drugList: [], // 处方药品列表
        drugBox: {
          timeStamp: '',
          id: '',
          list: []
        },
        multipleSelection: []
      }
    },
    methods: {
      // 增方
      prescriptionAddHandle() {
        let obj = {
          prescriptionType: '1',
          prescriptionOrderNumber: '',
          prescriptionCost: '',
          prescriptionDiagnose: '',
          drugList: [{
            id: 1,
            drugsName: '',
            dose: '',
            portion: '',
            dosage: '',
            usage: '',
            frequency: '',
            entrust: ''
          }]
        }
        this.prescriptionList.push(obj)
      },
      // 删方
      prescriptionDelHandle() {
        this.prescriptionList.splice(this.currentIndex, 1)
      },
      // 处方选中事件
      prescriptionClickHandle(item, index) {
        this.currentIndex = index
        this.drugList = item.drugList
        this.drugListVisible = true
        if (item.id) {
          // 已存处方
        } else {
          // 新增处方
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      // 增药
      drugAddHandle() {
        let timeStamp = new Date().getTime()
        let obj = {
          timeStamp: timeStamp,
          id: '',
          drugsName: '',
          dose: '',
          portion: '',
          dosage: '',
          usage: '',
          frequency: '',
          entrust: ''
        }
        this.drugList.push(obj)
      },
      // 删药
      drugDelHandle() {
        let arr = this.drugList.filter(item => {
          let result = this.multipleSelection.some(rowItem => {
            if (rowItem.timeStamp == item.timeStamp) {
              return true
            }
          })
          if (!result) {
            return item
          }
        })
        this.drugList = [...arr]
        // 处方金额计算
      },

      // 药品行获取焦点，打开搜索药品框
      inputFocusHandle(row) {
        this.drugBox = {
          timeStamp: row.timeStamp,
          id: row.id || '',
          list: []
        }
      },

      // 输入药品关键字
      inputChange(row) {
        this.visible = true
        let obj = {
          id: 1,
          drugsName: '第一',
          dose: '2',
          portion: '8',
          dosage: '12',
          usage: '说的是',
          frequency: '3',
          entrust: '说哭就哭'
        }
        let list = [{
          id: 2,
          drugsName: '第二',
          dose: '2',
          portion: '8',
          dosage: '12',
          usage: '说的是',
          frequency: '3',
          entrust: '说哭就哭'
        }]
        list.push(obj)
        this.$set(this.drugBox, 'list', list)
        this.drugBox = {...this.drugBox}
      },
      // 选中药品
      checkitem(row) {
        row.timeStamp = this.drugBox.timeStamp
        this.drugList.forEach((item, index) => {
          if (item.timeStamp == this.drugBox.timeStamp) {
            this.drugList[index] = row
          }
        })
        this.drugList = [...this.drugList]
        this.visible = false

        // 处方金额计算
      },
      // 点击关闭药品搜索框
      drugAlertCloseHandle(){
        this.visible = false
      },
      // 处方金额计算
    }
  }
</script>

<style>
  .drugs-alert {
    width: 800px;
    height: 500px;
    background-color: #fff;
    border: solid 1px #e7e7e7;
    position: fixed;
    top: 150px;
    right: 50px;
    z-index: 10000;
  }

  .recipel-card {
    background-color: #d1efef;
    margin-bottom: 5px;
    padding: 3px 10px;
  }

  .recipel-card.active {
    background-color: #7cdfd1;
  }
</style>
